package org.fioma.tree;

import java.util.ArrayList;
import java.util.List;

public class SimplePath<N extends Node<? extends LabelRef, ?, ?, ?>> implements Path<N> {

	private static ArrayList<SimplePath> pool = new ArrayList<SimplePath>(400);
	private static int nextObj = 0;

	ArrayList<N> nodes;
	int length;

	private SimplePath() {
		nodes = new ArrayList<N>();
		length = 0;
	}

	@Override
	public void addNode(N node) {
		nodes.add(node);
		length += node.getLabel().getLength();
	}

	@Override
	public int getLabelLength() {
		return length;
	}

	@Override
	public List<N> getNodes() {
		return nodes;
	}

	public static SimplePath getInstance() {
		if (nextObj >= pool.size()) {
			addValuesToPool(100);
		}
		SimplePath ret = pool.get(nextObj);
		nextObj++;
		ret.nodes.clear();
		ret.length = 0;
		return ret;
	}

	public static void freePool() {
		nextObj = 0;
	}

	public static void addValuesToPool(int qtty) {
		for (int i = 0; i < qtty; i++) {
			pool.add(new SimplePath());
		}
	}

}
